package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.DynamicReply;
import com.zaofan.bean.DynamicReplyDTO;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.Dynamic;
import com.zaofan.mobile.bean.DynamicDetail;
import com.zaofan.service.DynamicReplyService;
import com.zaofan.service.DynamicService;
import com.zaofan.utils.JsonUtils;

/**
 *
 */
@WebServlet("/mobile/dynamic")
public class DynamicController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private DynamicService dynamicService = new DynamicService();
	private DynamicReplyService replyService = new DynamicReplyService();

	/**
	 * 最新动态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void new_dynamic(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<Dynamic> dynamics = dynamicService.getNewDynamics(begin, end);
			JsonUtils.writeSuccessJson("查询成功", dynamics, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 精选动态接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void good_dynamic(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数有误!!", response);
				return;
			}
			List<com.zaofan.mobile.bean.Dynamic> datas = dynamicService.findJingxuanDynamic(begin, end);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 评论动态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void reply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String userId_s = request.getParameter("userId");
			String id_s = request.getParameter("id");
			String content = request.getParameter("content");
			Integer userId = 0;
			Integer id = 0;
			try {
				userId = Integer.valueOf(userId_s);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			dynamicService.replyDynamic(userId, id, content);
			JsonUtils.writeSuccessJson("发布成功", "", response);
			return;
		} catch (ServiceException e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("发布失败", response);
		}
		JsonUtils.writeBeanFailJson("发布失败!!", response);
	}

	/**
	 * 赞 动态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void praise(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String userId_s = request.getParameter("userId");
			String id_s = request.getParameter("id");
			Integer userId = 0;
			Integer id = 0;
			try {
				userId = Integer.valueOf(userId_s);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			DynamicReply dynamicReply = this.replyService.findPraiseReplyByUserIdAndReplyId(userId, id);
			if (dynamicReply != null) {
				replyService.delReply(dynamicReply);
				JsonUtils.writeSuccessJson("取消赞成功", "", response);
				return;
			}
			dynamicService.praiseDynamic(userId, id);
			JsonUtils.writeSuccessJson("赞成功", "", response);
			return;
		} catch (ServiceException e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("发布失败", response);
		}
		JsonUtils.writeBeanFailJson("发布失败!!", response);
	}

	/**
	 * 动态详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			String userIds = request.getParameter("userId");
			int id = 0;
			int userId = 0;
			try {
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeBeanFailJson("查询失败", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				userId = 0;
			}
			DynamicDetail dynamic = dynamicService.getDynamicDetail(id);
			Map<String, Object> data = new HashMap<>();
			data.put("dynamic", dynamic);
			if (replyService.findPraiseReplyByUserIdAndReplyId(userId, id) == null) {
				data.put("isPraise", false);
			} else {
				data.put("isPraise", true);
			}
			JsonUtils.writeSuccessJson("查询成功", data, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 动态回复列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void replys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("id");
			Integer begin = 0;
			Integer end = 10;
			Integer id = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicReplyDTO> datas = replyService.findByDynamicIdWithOther(begin + "", end + "", id + "", 1 + "",
					null);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 动态 赞 列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void praises(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("id");
			Integer begin = 0;
			Integer end = 10;
			Integer id = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicReplyDTO> datas = replyService.findByDynamicIdWithOther(begin + "", end + "", id + "", 0 + "",
					null);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 我所关注的人的动态列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void follow_dynamic(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String userIds = request.getParameter("userId");
			Integer begin = 0;
			Integer end = 10;
			Integer userId = 0;
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<Dynamic> dynamics = dynamicService.getFollowDynamics(begin, end, userId);
			JsonUtils.writeSuccessJson("查询成功", dynamics, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 未读信息个数接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void unread(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId");
			Integer userId = 0;
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("您还未登录,请重新登录试试!!", response);
				return;
			}
			Long replyCount = replyService.countUnRead(userId, 1);
			Long praiseCount = replyService.countUnRead(userId, 0);
			Map<String, Object> data = new HashMap<>();
			data.put("replyCount", replyCount);
			data.put("praiseCount", praiseCount);
			JsonUtils.writeSuccessJson("查询成功", data, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 未读列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void unread_reply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("userId");
			Integer begin = 0;
			Integer end = 10;
			Integer id = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicReplyDTO> datas = replyService.findByUserIdWithOther(begin + "", end + "", id + "", 1 + "",
					0 + "");
			if (datas != null) {
				for (DynamicReplyDTO dto : datas) {
					Dynamic dynamic = dynamicService.findDynamicWithOtherById(dto.getReplyId());
					dto.setDynamic(dynamic);
				}
			}
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 未读列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void unread_praise(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("userId");
			Integer begin = 0;
			Integer end = 10;
			Integer id = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicReplyDTO> datas = replyService.findByUserIdWithOther(begin + "", end + "", id + "", 0 + "",
					0 + "");
			if (datas != null) {
				for (DynamicReplyDTO dto : datas) {
					Dynamic dynamic = dynamicService.findDynamicWithOtherById(dto.getReplyId());
					dto.setDynamic(dynamic);
				}
			}
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	public void read_reply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = 0;
			try {
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			replyService.readReply(id);
			JsonUtils.writeSuccessJson("操作成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

}
